﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.DBUtilities;
using ShopB2C.DBUtilities.SqlDbHelper;
using ShopB2C.Entity;
using ShopB2C.Interfaces;

namespace ShopB2C.SQLDataAccess {
    public class ImageRepeat : IImageRepeat {

        #region Const Variables

        private const string _irAppend = "Image_Repeat_append";
        private const string _irUpdate = "Image_Repeat_Update";
        private const string _irDelete = "Image_Repeat_Delete";
        private const string _irSelectByIds = "Image_Repeat_SelectByIds";
        private const string _irSelect = "Image_Repeat_Select";
        private const string _irBatchUpdateSequence = "Image_Repeat_BatchUpdateSequence";

        #endregion

        public ImageRepeat(string connectionString) {
            DbBase.ConnectionString = connectionString;
        }

        #region ImageRepeat

        public void ImageRepeatAppend(ImageRepeatEntity ire) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@Name", 20, ire.Name).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Int32>("@Sequence", ire.Sequence).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@LinkUrl", 150, ire.LinkUrl).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Description", 50, ire.Description).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Alt", 50, ire.Alt).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Boolean>("@IsUse", ire.IsUse).SqlDbType = SqlDbType.Bit;
            SqlDbProcedure.ExecuteNonQuery(_irAppend, sqlParams.ToArray);
        }

        public void ImageRepeatUpdate(ImageRepeatEntity ire) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@IId", ire.IId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Name", 20, ire.Name).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Int32>("@Sequence", ire.Sequence).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@LinkUrl", 150, ire.LinkUrl).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Description", 50, ire.Description).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Alt", 50, ire.Alt).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Boolean>("@IsUse", ire.IsUse).SqlDbType = SqlDbType.Bit;
            SqlDbProcedure.ExecuteNonQuery(_irUpdate, sqlParams.ToArray);
        }

        public void ImageRepeatDelete(string irIds) {
            SqlDbProcedure.ExecuteNonQuery(_irDelete,
                DbParams<SqlParameter>.CreateParameter<string>("@IrIds", irIds));
        }

        public List<ImageRepeatEntity> GetImageRepeatList(string irIds) {
            List<ImageRepeatEntity> listResult = null;
            SqlDataReader dr;
            if (string.IsNullOrEmpty(irIds)) {
                dr = SqlDbProcedure.ExecuteReader(_irSelectByIds, null);
            }
            else {
                dr = SqlDbProcedure.ExecuteReader(_irSelectByIds,
                    DbParams<SqlParameter>.CreateParameter<string>("@IrIds", irIds));
            }
            if (dr.HasRows) {
                listResult = new List<ImageRepeatEntity>();
                while (dr.Read()) {
                    listResult.Add(new ImageRepeatEntity() {
                        IId = (int)dr["IId"],
                        Name = dr["Name"].ToString(),
                        Sequence = (int)dr["Sequence"],
                        LinkUrl = dr["LinkUrl"].ToString(),
                        Alt = dr["Alt"].ToString(),
                        Description = dr["Description"].ToString(),
                        IsUse = (bool)dr["IsUse"]
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public ImageRepeatEntity GetImageRepeatById(int irId) {
            ImageRepeatEntity ire = null;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_irSelect,
                DbParams<SqlParameter>.CreateParameter<int>("@IId", irId));
            if (dr.HasRows) {
                if (dr.Read()) {
                    ire = new ImageRepeatEntity() {
                        IId = (int)dr["IId"],
                        Name = dr["Name"].ToString(),
                        Sequence = (int)dr["Sequence"],
                        LinkUrl = dr["LinkUrl"].ToString(),
                        Alt = dr["Alt"].ToString(),
                        Description = dr["Description"].ToString(),
                        IsUse = (bool)dr["IsUse"]
                    };
                }
            }
            dr.Close();
            dr.Dispose();
            return ire;
        }

        public void ImageRepeatBatchUpdateSequence(string irIds, string sequences) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@IrIds", 4000, irIds).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Sequences", 4000, sequences).SqlDbType = SqlDbType.NVarChar;
            SqlDbProcedure.ExecuteNonQuery(_irBatchUpdateSequence, sqlParams.ToArray);
        }

        #endregion
    }
}
